/**
    ATClab - scriptable Air Traffic Control simulations.
    Copyright (C) 2010 openatclab@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

#ifndef __XMLSTRING_HDR__ 
#define __XMLSTRING_HDR__ 

#include <string>

#ifndef WIN32
namespace std{

	template<> 
	struct char_traits<XMLCh>
	{
		typedef XMLCh char_type;

		static void 
		assign(char_type& __c1, const char_type& __c2)
		{ __c1 = __c2; }

		static size_t
		length(const char_type* __s)
		{
			const char_type* __p = __s;
			while(*__p) ++__p;
			return(__p - __s);
		}

		static char_type*
		copy(char_type* __s1, const char_type* __s2, size_t __n)
		{
			return (char_type*)
				memcpy(__s1, __s2, __n * sizeof(char_type)); 
		}
		
	};

} 
#endif

namespace xml{
	
	typedef std::basic_string<XMLCh> xmlstring;
	
	std::string xml2std_string(const XMLCh* const);
	std::string xml2std_string(const xmlstring&);

	xmlstring std2xml_string(const char* const);
	xmlstring std2xml_string(const std::string&);


	inline std::string xml2std_string(const xmlstring& sxml){
		return xml2std_string(sxml.c_str());
	}
	
	inline std::string xml2std_string(const XMLCh* const sxml)
	{
		if(!sxml)return std::string();

		char* sptr = xercesc::XMLString::transcode(sxml);
		try{
			std::string rv(sptr);
			xercesc::XMLString::release(&sptr);
			return rv;
		}catch(...){
			xercesc::XMLString::release(&sptr);
			throw;
		}
	}
	
	inline xmlstring std2xml_string(const std::string& sstd){
		return std2xml_string(sstd.c_str());
	}
	
	inline xmlstring std2xml_string(const char* const sstd)
	{
		XMLCh* sptr = xercesc::XMLString::transcode(sstd);
		try{
			xmlstring rv(sptr);
			xercesc::XMLString::release(&sptr);
			return rv;
		}catch(...){
			xercesc::XMLString::release(&sptr);
			throw;
		}
	}
	
}

#endif 
